package com.itheima.controller;

import com.itheima.VO.CheckItemVO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;

import com.itheima.pojo.CheckItem;
import com.itheima.pojo.PageParm;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 查询全部检查项
     * @param checkItem
     * @return
     */
    @GetMapping("/findAll")
    public Result list(CheckItem checkItem){
        log.info("查询全部检查项");
        List<CheckItemVO> list=checkItemService.list(checkItem);

        return Result.success(list);
    }

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result insert(CheckItem checkItem){
        log.info("新增检查项:{}",checkItem);
        checkItemService.insert(checkItem);

        return Result.success();
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id){
        log.info("根据id删除检查项");
        checkItemService.deleteById(id);
        return Result.success();
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestBody CheckItem checkItem){
        log.info("编辑检查项：{}",checkItem);
        checkItemService.edit(checkItem);
        return Result.success();
    }

    /**
     * 分页查询
     * @param pageParm
     * @return
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody PageParm pageParm) {
        log.info("分页查询:{},{}",pageParm.getCurrentPage(),pageParm.getPageSize());
        PageResult page = checkItemService.page(pageParm.getCurrentPage(),pageParm.getPageSize());
        return page;
    }

    /**
     * 根据id查询检查项:{}
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("id")@RequestBody Integer id){
        log.info("根据id查询检查项:{}",id);
        CheckItem checkItem=checkItemService.findById(id);
        return Result.success(checkItem);
    }

    /**
     * 根据groupid查询itemid表
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam("checkgroupId") @RequestBody Integer id){
        log.info("根据groupid查询itemid表");
        List<CheckItem> list=checkItemService.getItemById(id);
        return Result.success(list);
    }

}

